package com.copa.module.goods.manage.impl;

import cn.hutool.core.util.ObjectUtil;
import com.copa.domain.ResponseDto;
import com.copa.module.goods.domain.entity.CategoryEntity;
import com.copa.module.goods.domain.entity.GoodsAttributeEntity;
import com.copa.module.goods.domain.entity.GoodsProductEntity;
import com.copa.module.goods.domain.entity.GoodsSpecificationsEntity;
import com.copa.module.goods.domain.vo.GoodSpecificationsVo;
import com.copa.module.goods.domain.vo.GoodsAttributeVo;
import com.copa.module.goods.domain.vo.GoodsProductVo;
import com.copa.module.goods.domain.vo.GoodsUpdateVo;
import com.copa.module.goods.manage.GoodsManage;
import com.copa.module.goods.mapper.CategoryDao;
import com.copa.module.goods.mapper.GoodsAttributeDao;
import com.copa.module.goods.mapper.GoodsProductDao;
import com.copa.module.goods.mapper.GoodsSpecificationsDao;
import com.google.common.collect.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author copa
 * @createDate 2021-01-08 12:31
 * @function
 */
@Service
public class GoodsManageImpl implements GoodsManage {

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private GoodsAttributeDao goodsAttributeDao;

    @Autowired
    private GoodsProductDao goodsProductDao;

    @Autowired
    private GoodsSpecificationsDao goodsSpecificationsDao;

    @Override
    public ResponseDto<CategoryEntity> findCategoryByCategoryId(Long categoryId) {
        CategoryEntity entity = categoryDao.selectById(categoryId);
        if (ObjectUtil.isNotNull(entity)) {
            return ResponseDto.success(entity);
        }
        return ResponseDto.errorMsg("抱歉，找不到您所查的类目！");
    }

    @Override
    public ResponseDto<Boolean> addGoodsAttribute(List<GoodsAttributeVo> goodsAttributeVo, Long goodsId) {

        Boolean flag = goodsAttributeDao.insertGoodsAttributeVoList(goodsAttributeVo, goodsId);
        if (flag) {
            return ResponseDto.successMsg("操作成功！");
        }
        return ResponseDto.errorMsg("系统繁忙，请稍后再试！");
    }

    @Override
    public ResponseDto<Boolean> addGoodsProduct(List<GoodsProductVo> goodsProductVo, Long goodsId) {
        Boolean flag = goodsProductDao.insertGoodsProductVoList(goodsProductVo, goodsId);
        if (flag) {
            return ResponseDto.successMsg("操作成功！");
        }
        return ResponseDto.errorMsg("系统繁忙，请稍后再试！");
    }

    @Override
    public ResponseDto<Boolean> addGoodsSpecifications(List<GoodSpecificationsVo> GoodSpecificationsVo, Long goodsId) {
        Boolean flag = goodsSpecificationsDao.insertGoodsSpecificationsList(GoodSpecificationsVo, goodsId);
        if (flag) {
            return ResponseDto.successMsg("操作成功！");
        }
        return ResponseDto.errorMsg("系统繁忙，请稍后再试！");
    }

    @Override
    public ResponseDto<List<GoodsProductEntity>> findGoodsProductByGoodsId(Long goodsId) {
        List<GoodsProductEntity> goodsProductEntities = goodsProductDao.findGoodsProductByGoodsId(goodsId);
        if (ObjectUtil.isNotNull(goodsProductEntities) && ObjectUtil.length(goodsProductEntities) > 0) {
            return ResponseDto.success(goodsProductEntities);
        }
        return ResponseDto.errorMsg("抱歉，没查到该商品的sku！");
    }

    @Override
    public ResponseDto<List<GoodsAttributeEntity>> findGoodsAttributeByGoodsId(Long goodsId) {
        List<GoodsAttributeEntity> goodsAttributeEntities = goodsAttributeDao.findGoodsAttributeByGoodsId(goodsId);
        if (ObjectUtil.isNotNull(goodsAttributeEntities) && ObjectUtil.length(goodsAttributeEntities) > 0) {
            return ResponseDto.success(goodsAttributeEntities);
        }
        return ResponseDto.errorMsg("抱歉，没查到该商品的规格！");
    }

    @Override
    public ResponseDto<String> putGoodsAttribute(GoodsUpdateVo goodsUpdateVo) {
        if (ObjectUtil.isNull(goodsUpdateVo.getGoodsAttributeVo())
                || ObjectUtil.length(goodsUpdateVo.getGoodsAttributeVo()) == 0) {
            return ResponseDto.errorMsg("请添加属性！");
        }
        ResponseDto<List<GoodsAttributeEntity>> attributeRes = findGoodsAttributeByGoodsId(goodsUpdateVo.getId());

        HashSet<Long> attributeIds = Sets.newHashSet();
        if (attributeRes.isSuccess()) {
            attributeRes.getData().forEach(attribute -> attributeIds.add(attribute.getId()));
        }
        Set<Long> fromAttributeIds = goodsUpdateVo.getGoodsAttributeVo().stream()
                .map(GoodsAttributeVo::getId)
                .collect(Collectors.toSet());
        attributeIds.removeAll(fromAttributeIds);
        if (ObjectUtil.length(attributeIds) > 0) {
            ResponseDto<Boolean> deleteAttributeRes = deleteAttribute(new ArrayList<>(attributeIds));
            if (deleteAttributeRes.isNotSuccess()) {
                return ResponseDto.errorMsg(deleteAttributeRes.getMsg());
            }
        }

        // 新增
        for (GoodsAttributeVo goodsAttributeVo : goodsUpdateVo.getGoodsAttributeVo()) {
            if (goodsAttributeVo.getId() == null) {
                goodsAttributeDao.insertGoodsAttributeVoList(Collections.singletonList(goodsAttributeVo), goodsUpdateVo.getId());
            }
        }
        return ResponseDto.successMsg("处理商品规格成功！");
    }

    @Override
    public ResponseDto<String> putGoodsProduct(GoodsUpdateVo goodsUpdateVo) {
        if (ObjectUtil.isNull(goodsUpdateVo.getGoodsProductVo())
                || ObjectUtil.length(goodsUpdateVo.getGoodsProductVo()) == 0) {
            return ResponseDto.errorMsg("请添加sku！");
        }
        ResponseDto<List<GoodsProductEntity>> productRes = findGoodsProductByGoodsId(goodsUpdateVo.getId());

        HashSet<Long> productIds = Sets.newHashSet();
        if (productRes.isSuccess()) {
            productRes.getData().forEach(product -> productIds.add(product.getId()));
        }
        Set<Long> fromProductIds = goodsUpdateVo.getGoodsProductVo().stream()
                .map(GoodsProductVo::getId)
                .collect(Collectors.toSet());
        productIds.removeAll(fromProductIds);
        if (ObjectUtil.length(productIds) > 0) {
            ResponseDto<Boolean> deleteProductRes = deleteProducts(new ArrayList<>(productIds));
            if (deleteProductRes.isNotSuccess()) {
                return ResponseDto.errorMsg(deleteProductRes.getMsg());
            }
        }


        // 新增
        for (GoodsProductVo goodsProductVo : goodsUpdateVo.getGoodsProductVo()) {
            if (goodsProductVo.getId() == null) {
                goodsProductDao.insertGoodsProductVoList(Collections.singletonList(goodsProductVo), goodsUpdateVo.getId());
            } else {
                goodsProductDao.updateGoodsProductVoById(goodsProductVo);
            }
        }
        return ResponseDto.successMsg("处理sku成功！");
    }

    @Override
    public ResponseDto<String> putGoodsSpecifications(GoodsUpdateVo goodsUpdateVo) {
        if (ObjectUtil.isNull(goodsUpdateVo.getGoodSpecificationsVo())
                || ObjectUtil.length(goodsUpdateVo.getGoodSpecificationsVo()) == 0) {
            return ResponseDto.errorMsg("请添加规格！");
        }
        ResponseDto<List<GoodsSpecificationsEntity>> specificationsRes =
                findGoodsSpecificationsResByGoodsId(goodsUpdateVo.getId());

        HashSet<Long> specificationsIds = Sets.newHashSet();
        if (specificationsRes.isSuccess()) {
            specificationsRes.getData().forEach(specifications -> specificationsIds.add(specifications.getId()));
        }
        Set<Long> fromSpecificationsIds = goodsUpdateVo.getGoodSpecificationsVo().stream()
                .map(GoodSpecificationsVo::getId)
                .collect(Collectors.toSet());
        specificationsIds.removeAll(fromSpecificationsIds);
        if (ObjectUtil.length(specificationsIds) > 0) {
            ResponseDto<Boolean> deleteSpecificationsRes = deleteSpecifications(new ArrayList<>(specificationsIds));
            if (deleteSpecificationsRes.isNotSuccess()) {
                return ResponseDto.errorMsg(deleteSpecificationsRes.getMsg());
            }
        }

        // 新增
        for (GoodSpecificationsVo goodSpecificationsVo : goodsUpdateVo.getGoodSpecificationsVo()) {
            if (goodSpecificationsVo.getId() == null) {
                goodsSpecificationsDao.insertGoodsSpecificationsList(Collections.singletonList(goodSpecificationsVo), goodsUpdateVo.getId());
            } else {
                goodsSpecificationsDao.updateGoodspecificationVoById(goodSpecificationsVo);
            }
        }
        return ResponseDto.successMsg("处理规格成功！");
    }

    @Override
    public ResponseDto<List<GoodsSpecificationsEntity>> findGoodsSpecificationsByGoodsId(Long goodsId) {
        List<GoodsSpecificationsEntity> goodsSpecificationsEntities = goodsSpecificationsDao.findSpecificationsByGoodsId(goodsId);
        if (ObjectUtil.isNotNull(goodsSpecificationsEntities) && ObjectUtil.length(goodsSpecificationsEntities) > 0) {
            return ResponseDto.success(goodsSpecificationsEntities);
        }
        return ResponseDto.errorMsg("抱歉，没查到该商品的规格！");
    }

    private ResponseDto<Boolean> deleteSpecifications(List<Long> specificationsIds) {
        Boolean flag = goodsSpecificationsDao.deleteIds(specificationsIds);
        if (flag) {
            return ResponseDto.successMsg("操作成功！");
        }
        return ResponseDto.errorMsg("系统繁忙，请稍后再试！");
    }

    private ResponseDto<List<GoodsSpecificationsEntity>> findGoodsSpecificationsResByGoodsId(Long goodsId) {
        List<GoodsSpecificationsEntity> goodsSpecificationsEntities = goodsSpecificationsDao.findSpecificationsByGoodsId(goodsId);
        if (ObjectUtil.isNotNull(goodsSpecificationsEntities)
                && ObjectUtil.length(goodsSpecificationsEntities) > 0) {
            return ResponseDto.success(goodsSpecificationsEntities);
        }
        return ResponseDto.errorMsg("抱歉，没查到该商品的规格！");
    }

    private ResponseDto<Boolean> deleteProducts(List<Long> productIds) {
        Boolean flag = goodsProductDao.deleteIds(productIds);
        if (flag) {
            return ResponseDto.successMsg("操作成功！");
        }
        return ResponseDto.errorMsg("系统繁忙，请稍后再试！");
    }

    private ResponseDto<Boolean> deleteAttribute(List<Long> attributeIds) {
        Boolean flag = goodsAttributeDao.deleteIds(attributeIds);
        if (flag) {
            return ResponseDto.successMsg("操作成功！");
        }
        return ResponseDto.errorMsg("系统繁忙，请稍后再试！");
    }



}
